package dao.impl;

import bean.Page;
import bean.Test;
import dao.mapper.TestMapper;
import dao.TestDAO;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Vector;

@Repository(value = "dao")
public class TestDAOImpl extends SqlSessionDaoSupport implements TestDAO {
    public TestDAOImpl(@Autowired @Qualifier("sessionFactory") SqlSessionFactory factory) {
        setSqlSessionFactory(factory);
    }

    @Override
    public boolean add(String var) {
        return getSqlSession().getMapper(TestMapper.class).ins(var);
    }

    @Override
    public boolean mod(Test object) {
        return getSqlSession().getMapper(TestMapper.class).upd(object);
    }

    @Override
    public boolean rem(int id) {
        return getSqlSession().getMapper(TestMapper.class).del(id);
    }

    @Override
    public Test que(int id) {
        return getSqlSession().getMapper(TestMapper.class).selById(id);
    }

    @Override
    public List<Test> que(String keywords) {
        return getSqlSession().getMapper(TestMapper.class).selByKw(keywords);
    }

    @Override
    public Page query(Page page) {
        SqlSession session = getSqlSession();
        int recordTotal = session.getMapper(TestMapper.class).sltByKw_count(page);
        page.setRecordTotal(recordTotal);
        Vector<Test> data = session.getMapper(TestMapper.class).sltByKw(page);
        page.setData(data);
        return page;
    }
}
